package org.iteam.xlz.manager.action.sys;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.commons.security.sha.ShaUtil;
import org.iteam.core.model.sys.SysUser;
import org.iteam.core.service.sys.SysUserService;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;

import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/sys/password-index.html")})
public class SysPasswordAction extends AbstractModuleAction<SysUser> implements ModelDriven<SysUser>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3838856382735995405L;
	private final String PATH="/sys/password";
	
	@Action(PATH+"/index")
	public String index() {
		model.setAccount(ShiroUtil.currentAccount());
		return INDEX;
	}
	
	@Action(PATH+"/update")
	public String update(){
		boolean flag=false;
		if(StringUtils.isNotBlank(pwd)||StringUtils.isNotBlank(newPwd)){
			try {
				flag=SysUserService.getInstance().updatePwd(ShiroUtil.currentAccount(),ShaUtil.toSha256String(pwd),ShaUtil.toSha256String(newPwd));
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		map.put("state",flag);
		return MAP;
	}

	@Override
	public SysUser getModel() {
		if(null==model){
			model=new SysUser();
		}
		return model;
	}
	
	private String pwd;
	private String newPwd;

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
	
}
